<template>
  <div class="login-container">
    <div class="login-box">
      <h2>Login</h2>
      <form @submit.prevent="login">
        <div class="input-group">
          <label for="username">Username</label>
          <input type="username" id="username" v-model="username" placeholder="Enter your username" required />
        </div>
        <div class="input-group">
          <label for="password">Password</label>
          <input type="password" id="password" v-model="password" placeholder="Enter your password" required />
        </div>
        <button type="submit" class="login-btn">Login</button>
      </form>
      <p class="signup-link">Don't have an account? <router-link to="/register">Sign up</router-link></p>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../stores/userStore';
import {useServer} from '../components/ts/useServer';  
const username = ref('');
const password = ref('');
const userStore = useUserStore();
const router = useRouter();
const {post} = useServer();

const login = () => {
  if (!(username.value && password.value)) {alert('Please fill in all fields');return;}
  post('login', { username: username.value, password: password.value }).then(res => {
    if (res.success) {
      userStore.login(res.userID, res.username);
      router.push('/myprojects');
    } else {
      alert('Invalid email or password');
    }
  });
};
</script>

<style scoped lang="scss">
/* 基本布局 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  //background-color: #f0f4f8;
  background: url('@/assets/images/loginback.png') no-repeat center center fixed;
  background-size: cover;
}

.login-box {
  background-color: #fff;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  text-align: center;
}

h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
}

/* 输入框样式 */
.input-group {
  margin-bottom: 20px;
  text-align: left;
}

.input-group label {
  font-size: 14px;
  color: #666;
}

.input-group input {
  width: 100%;
  padding: 10px;
  margin-top: 8px;
  border-radius: 8px;
  border: 1px solid #ccc;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s ease;
}

.input-group input:focus {
  border-color: #007bff;
}

/* 按钮样式 */
.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 6px rgba(0, 123, 255, 0.3);
}

.login-btn:hover {
  background-color: #0056b3;
  transform: translateY(-2px);
  box-shadow: 0 6px 8px rgba(0, 123, 255, 0.4);
}

.login-btn:active {
  transform: translateY(0);
  box-shadow: 0 4px 6px rgba(0, 123, 255, 0.3);
}

/* 注册链接 */
.signup-link {
  margin-top: 20px;
  font-size: 14px;
}

.signup-link a {
  color: #007bff;
  text-decoration: none;
}

.signup-link a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 20px;
  }

  h2 {
    font-size: 20px;
  }

  .login-btn {
    font-size: 16px;
  }
}
</style>
